//
//  AELabel.cpp
//  GameJam-Mac
//
//  Created by Clover Sin on 12/28/11.
//  Copyright (c) 2011 Snow Poem Society. All rights reserved.
//

#include "AELabel.h"

AELabel::AELabel(AEControl* parent, AETheme* theme): 
    AEControl(parent, theme), 
    _text(""), 
    _textActor(NULL)
{
    // Initialize textActor, which draws text actually.
    // It is not a child but surely a component, 
    //   so add it in the init() method and initialize first.
    _textActor = new TextActor(theme->labelFont, _text, theme->labelTextAlignment);
    _textActor->SetLayer(this->GetLayer() + 1);
    
    this->SetName("Label");
    this->setText(this->GetName()); // Default text is its name
    this->setControlPosition(_parentControl->GetPosition() + _offsetFromParent);
    this->SetAlpha(0.0f);
    this->SetSize(theme->labelSize);

}

AELabel::~AELabel()
{
    theWorld.Remove(_textActor);
    delete _textActor;
}

void AELabel::prepareForAddingToWorld()
{
    syncChildrenPosition();
    
    // TextActor is not a child but a component, 
    //   so sync it manually.
    syncPositionOfText();
    
    theWorld.Add(_textActor);
}

void AELabel::setControlPosition(Vector2 pos)
{
    this->SetPosition(pos);
    
    // Sync TextActor's position manually since it 
    //   is not a child control.
    syncPositionOfText();
    
    syncChildrenPosition();
}

void AELabel::setText(const String &text)
{
    _text = text;
    _textActor->SetDisplayString(text);
}

String AELabel::getText()
{
    return _text;
}

void AELabel::syncPositionOfText()
{
    Vector2 posInPixel = this->GetPosition();
    
    Vector2 pos = MathUtil::ScreenToWorld(posInPixel.X, posInPixel.Y);
    
    _textActor->SetPosition(pos);
}
